#include "stdio.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"

#include "stdlib.h"
#include "string.h"

#define READ_BUFFER_SIZE	100

int main(int argc, char *argv[])
{
	int sockfd;
	int err;
	int read_bytes;
	char read_buf[100];
	struct sockaddr_in svr_addr;

	svr_addr.sin_family 		= AF_INET;
	svr_addr.sin_addr.s_addr	= inet_addr("127.0.0.1");
	svr_addr.sin_port			= htons(8888);

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0){
		perror("socket failed");
		return -1;
	}

	err = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
	if(err < 0){
		perror("connect failed");
		return -1;
	}

	read_bytes = read(sockfd, read_buf, sizeof(read_buf));
	if(read_bytes < 0){
		printf("read error\n");
		return -1;
	}
	
	if(read_bytes == 0){
		printf("sever close connection\n");
		return -1;
	}

	printf("read bytes is %d\n", read_bytes);
	printf("content : %s\n", read_buf);

	close(sockfd);
	
	return 0;
}
